package homework.jdbc.dao;

import homework.jdbc.domain.User;

import java.sql.*;
import java.util.List;

/**
 * Created by Administrator on 2017/9/13.
 */
public class UserDao {
    private  static  Connection conn=null;
    public List<User> findAll() throws SQLException {
        Connection conn=JDBCutils.Connecti();
        Statement statement=conn.createStatement();
        String sql="select * from user";
        ResultSet result =statement.executeQuery(sql);
        User user=new User();
        user.setUid(result.getInt("uid"));
        user.setUsername(result.getString(2));
        user.setPassword(result.getString(3));
        return null;
    }
    public boolean insert(User user)  {
        Connection conn=JDBCutils.Connecti();
        String sql="update user username=?,password=? where uid=?";
        PreparedStatement ps=null;
        try{
           ps=conn.prepareStatement(sql);
            ps.setInt(1,user.getUid());
            ps.setString(2,user.getUsername());
            ps.setString(3,user.getPassword());
            int num=ps.executeUpdate();
            if(num==1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    private void uid() {
    }

    public  boolean update(){
        return  false;
    }
    public boolean delete(){
        return false;
    }
    public void findByld(int uid){
        PreparedStatement ps=null;
        try{
            String sql="select* from user where uid=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,uid);
            ResultSet result = ps.executeQuery();
            while (result.next()){

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
